#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::mem_fn;
using std::vector;
using std::for_each;

class Number
{
public:
    Number(int data = 0)
    : _data(data)
    {
    }

    void print()
    {
        cout << _data << "  ";
    }

    //判断偶数
    bool isEven()
    {
        return (0 == _data % 2);
    }

    //判断质数
    bool isPrime()
    {
        if(1 == _data)
        {
            return false;
        }

        for(int idx = 2; idx <= _data/2; ++idx)
        {
            if(0 == _data % idx )
            {
                return false;
            }
        }

        return true;
    }

    ~Number()
    {
    }
private:
    int _data;
};

void test()
{
    vector<Number> vec;
    for(size_t idx = 1; idx != 30; ++idx)
    {
        vec.push_back(Number(idx));
    }

    //对于C++中的算法库中函数，如果用到成员函数，需要使用
    //成员函数适配器进行包装一下
    Number nu(1);
    /* for_each(vec.begin(), vec.end(), mem_fn(&Number::print)); */
    for_each(vec.begin(), vec.end(), 
             std::bind(&Number::print, std::placeholders::_1));
    cout << endl;

    cout << endl;
    auto  it = remove_if(vec.begin(), vec.end(), 
                         mem_fn(&Number::isEven));
    vec.erase(it, vec.end());
    for_each(vec.begin(), vec.end(), mem_fn(&Number::print));
    cout << endl;

    cout << endl;
    auto  it2 = remove_if(vec.begin(), vec.end(), 
                         mem_fn(&Number::isPrime));
    vec.erase(it2, vec.end());
    for_each(vec.begin(), vec.end(), mem_fn(&Number::print));
    cout << endl;
}

int main(int argc, char **argv)
{
    test();
    return 0;
}

